<template>
    <div>
        <!--搜索框-->
        <el-card class="box-card">
            <div>
                <el-form size="small" :inline="true" :model="queryData" class="demo-form-inline" label-width="100px">
                    <el-form-item>
                        <el-input v-model="queryData.name" placeholder="联系人姓名"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="queryData.phone" placeholder="联系人手机号"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-select v-model="queryData.activityInstanceId" placeholder="线下活动名称">
                            <el-option
                                    v-for="item in instance"
                                    :key="item.activityInstanceId"
                                    :label="item.name"
                                    :value="item.activityInstanceId" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button size="small" type="primary" @click="queryDataHandle(pagePagination.currentPage,pagePagination.pageSize)">查询</el-button>
                        <el-button size="small" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
        <div class="interspaceH"></div>
        <!--table-->
        <el-card class="box-card">

            <el-button type="primary" @click="add">+ 用户信息添加</el-button>
<!--            <el-button type="primary" @click="importVisible = true">导入客户</el-button>-->

            <div class="interspaceH"></div>

            <br/>
            <div>
                <el-table @selection-change="tableRowChange" :data="tableData"
                          :header-cell-style="{background:'#F3F4F7',color:'#555'}" style="width: 100%">
                    <el-table-column
                            prop="name"
                            label="联系人姓名">
                    </el-table-column>
                    <el-table-column
                            prop="phone"
                            label="联系人手机号">
                    </el-table-column>
                    <el-table-column
                            prop="activityInstanceName"
                            label="线下活动名称">
                    </el-table-column>
                    <el-table-column
                            prop="createTime"
                            label="创建时间">
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                               @current-change="queryDataHandle($event,pagePagination.pageSize)"
                               :current-page="pagePagination.currentPage"
                               :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                               layout="total, sizes, prev, pager, next"
                               :total="pagePagination.total">
                </el-pagination>
            </div>
        </el-card>

      <el-dialog :title="title" :visible.sync="editFormVisible" :rules="rules" width="30%">
        <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
          <el-form-item label="联系人姓名" prop="name" label-width="120px">
            <el-input v-model="formData.name" placeholder="请输入联系人姓名" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="联系人手机号" prop="phone" label-width="120px">
            <el-input v-model="formData.phone" placeholder="请输入联系人手机号" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="线下活动" label-width="120px">
              <el-select v-model="formData.activityInstanceId" placeholder="选择线下活动">
                  <el-option
                          v-for="item in instance"
                          :key="item.activityInstanceId"
                          :label="item.name"
                          :value="item.activityInstanceId" />
              </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="title" @click="submitForm">提交</el-button>
            <el-button @click="quxiao">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
        <!--导入客户-->
        <el-dialog
                title="导入客户"
                :visible.sync="importVisible"
                width="30%">
            <el-upload
                    class="upload-demo"
                    style="display: flex;justify-content: center"
                    drag
                    ref="uploadExcel"
                    :show-file-list="false"
                    :on-success="uploadSuccess"
                    :action="$uri + '/api/information/importInformation/1_0'">
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
            <div style="display: flex;justify-content: center;align-items: center;margin-top: 20px">
                只能上传 xls/xlsx/csv 文件，且不超过10MB
                <el-button size="small" type="text" @click="down" style="padding: 0;font-size: 14px">点击此处下载模板</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

import Pagination from "@/components/Pagination"

export default {
    name: "user",
    components: {
        Pagination
    },
    data() {
        return {
          //title: '线下活动签到信息新建',
          title: '用户信息添加',
          editFormVisible: false,
            importVisible: false,
          queryData: {
            name: '',
            peopleName: '',
            phone: '',
            huodongName: '',
            shuxing: ''
          },
            instance: [],
          formData: {},
          tableData: [],
          formInline: {
            page: 1,
            limit: 10,
            varLable: '',
            varName: '',
            token: localStorage.getItem('logintoken')
          },
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            },
          rules: {
            name: [
              {required: true, message: '请输入活动名称', trigger: 'blur'},
              {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
            ],
            peopleName: [
              {required: true, message: '请输入联系人姓名', trigger: 'blur'}
            ],
            phone: [
              {required: true, message: '请输入联系人手机号', trigger: 'blur'}
            ],
            zhiwu: [
              {required: true, message: '请输入联系人职务', trigger: 'blur'}
            ],
            huodongName: [
              { required: true, message: '请选择线下活动', trigger: 'change' }
            ],
            shuxing: [
              { required: true, message: '请选择联系人属性', trigger: 'change' }
            ],
              activityInstanceId: [
                  {required:true, message: '请选择活动', trigger: 'change'}
              ]
          }
        }
    },
    mounted() {
        this.queryDataHandle(1,10)
    },
    methods: {
        //查询
        queryDataHandle(page = 1, row = 10){
            let payload = {
                pageNum: page,
                pageSize: row,
                activityInstanceType: '线下',
                name: this.queryData.name,
                phone: this.queryData.phone,
                activityInstanceId: this.queryData.activityInstanceId
            }
            let that = this
            //查询列表
            that.$request("/api/information/selectAll/1_0", payload, "POST").then((data) => {
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
                that.tableData = data.rows
            })

            this.$request("/api/wheel/getOffline/1_0", {}, "POST").then((data) => {
                that.instance = data.rows
            })
        },
        //重置
        resetQuery() {
            this.queryData = {
                data: []
            }
            this.queryDataHandle(1,10)
        },
        //导入
        down() {
            this.$requestFile('/api/excel/downloadTemp/1_0?fileType=clientActivity', "", "线下签到导入模板.xls")
        },
        uploadSuccess() {
            this.$message.success("导入成功");
            this.$refs.uploadExcel.clearFiles()
            this.importVisible = false
            this.queryDataHandle()
        },
      add() {
        this.editFormVisible = true
      },
      submitForm() {

          let information = {
              name: this.formData.name,
              phone: this.formData.phone,
              activityInstanceId: this.formData.activityInstanceId
          }
        let that=this
          that.$request("/api/information/insertUser/1_0", information, "POST").then((data) => {
              if (data.message == '操作成功'){
                  this.$message({
                      message: '添加成功',
                      type: 'success'
                  });
                  this.editFormVisible = false
                  this.queryDataHandle(1,10)
              }else {
                  this.$message({
                      message: '添加失败',
                      type: 'error'
                  });
                  this.editFormVisible = false
                  this.queryDataHandle(1,10)
              }
          })

      },
      quxiao() {
          this.editFormVisible = false
      },
        // 获取公司列表
        getdata(parameter) {
            this.loading = true
            // 模拟数据开始
            let res = {
                code: 0,
                msg: null,
                count: 1,
                tableData: [
                    {
                        peopleName: '张三',
                        phone: '15478954361',
                        name: '大润发',
                        zhiwu: '销售',
                        shuxing: '报名签到',
                        huodongName: '商场中秋活动',
                        biaoqian: '门店',
                        data: '2021-10-10 10:10:10'
                    },
                ]
            }
            this.loading = false
            this.listData = res.data
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.count
            // 模拟数据结束
        },
        // 分页插件事件
        callFather(parm) {
            this.formInline.page = parm.currentPage
            this.formInline.limit = parm.pageSize
            this.getdata(this.formInline)
        },
        tableRowChange(val) {
            if (val.length > 0) {
                this.titleCurrentPanel = true
            } else {
                this.titleCurrentPanel = false
            }
            this.multipleSelection = val;
        },
    }
}
</script>

<style scoped>

</style>
